package com.carsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.carsystem.entity.Member;
import com.carsystem.entity.MemberEchartsVo;
import com.carsystem.mapper.MemberMapper;
import com.carsystem.service.IMemberService;
import com.carsystem.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ykl
 * @since 2021-12-04
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Resource
    private MemberMapper memberMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Member> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        String sex = queryPageBean.getSex();
        String regTime = queryPageBean.getReg();
        String age = queryPageBean.getAge();

        int zero=0;
        int eighteen=0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        String nowTime = dateFormat.format(new Date());
        int time = Integer.parseInt(nowTime);

        if (age.contains("-")) {
            String[] split = age.split("-");

            int perAge = Integer.parseInt(split[0]);
            int nextAge = Integer.parseInt(split[1]);

            zero = time - perAge;
            eighteen = time - nextAge;
        }
        if (StringUtils.isNotBlank(queryString) && !sex.equals("不限") && !regTime.equals("不限") && !age.equals("不限")){
            queryWrapper.eq("sex",sex)
                    .between("birthday", eighteen +"-01-01", zero +"-12-31")
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString))
                    .and(reg -> reg.like("regTime",regTime+"%"));
        } else if (StringUtils.isNotBlank(queryString) && !sex.equals("不限") && !regTime.equals("不限")){
            queryWrapper.eq("sex",sex)
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString))
                    .and(reg -> reg.like("regTime",regTime+"%"));
        } else if (!sex.equals("不限") && !age.equals("不限") && !regTime.equals("不限")) {
            queryWrapper.eq("sex",sex)
                    .between("birthday", eighteen +"-01-01", zero +"-12-31")
                    .and(reg -> reg.like("regTime",regTime+"%"));

        } else if (StringUtils.isNotBlank(queryString) && !regTime.equals("不限")  && !age.equals("不限")) {
            queryWrapper
                    .between("birthday", eighteen +"-01-01", zero +"-12-31")
                    .and(reg -> reg.like("regTime",regTime+"%"))
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString));;

        } else if (StringUtils.isNotBlank(queryString) && !sex.equals("不限")  && !age.equals("不限")) {
            queryWrapper.eq("sex",sex)
                    .between("birthday", eighteen +"-01-01", zero +"-12-31")
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString));

        } else if (StringUtils.isNotBlank(queryString) && !sex.equals("不限")){
            queryWrapper.eq("sex",sex)
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString));
        }else if (StringUtils.isNotBlank(queryString) && !regTime.equals("不限")){
            queryWrapper.like("regTime",regTime+"%")
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString));
        }else if (!sex.equals("不限") && !regTime.equals("不限")){
            queryWrapper.eq("sex",sex)
                    .and(reg -> reg.like("regTime",regTime+"%"));
        }else if (StringUtils.isNotBlank(queryString)  && !age.equals("不限")){
            queryWrapper
                    .between("birthday",eighteen+"-01-01",zero+"-12-31")
                    .and(member_name -> member_name.like("member_name",queryString).or().like("phoneNumber",queryString));

        }else if (!sex.equals("不限")  && !age.equals("不限")){
            queryWrapper.eq("sex",sex)
                    .between("birthday",eighteen+"-01-01",zero+"-12-31");

        }else if (!regTime.equals("不限")  && !age.equals("不限")) {
            queryWrapper
                    .between("birthday", eighteen + "-01-01", zero + "-12-31")
                    .and(reg -> reg.like("regTime", regTime + "%"));

        }
        else if (!sex.equals("不限")){
            queryWrapper.eq("sex",sex);

        }else if (!regTime.equals("不限")){
            queryWrapper.like("regTime",regTime+"%");

        }else if (queryString!=null && queryString.length()>0){
            queryWrapper.like("member_name",queryString)
                    .or().like("phoneNumber",queryString);

        }else if (!age.equals("不限")){
            queryWrapper.between("birthday",eighteen+"-01-01",zero+"-12-31");
        }

        Page<Member> memberPage = memberMapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(memberPage.getTotal(), memberPage.getRecords());
        return pageResult;
    }

    @Override
    public Result addMember(Member member) {
        member.setStatus("正常");
        member.setRegTime(new Date());
        member.setBirthday(new Date());
        int insert = memberMapper.insert(member);
        if (insert==1){
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
    }

    @Override
    public Result updateMember(Member member) {
        int i = memberMapper.updateById(member);
        if (i==1){
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }

    @Override
    public Result deleteMember(int memberId) {
        int i = memberMapper.deleteById(memberId);
        if (i==1){
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
    }

    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        QueryWrapper<Member> queryWrapper=new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        String sex = queryPageBean.getSex();
        if(queryString!=null && queryString.length()>0 && !queryString.equals("null")){
            queryWrapper.or().like("phoneNumber",queryString)
                    .or().like("member_name",queryString);
        }
        if(queryString!=null && queryString.length()>0 && !queryString.equals("null") && sex.equals("男")){
            queryWrapper.or().like("phoneNumber",queryString)
                    .or().like("member_name",queryString).or().eq("sex",sex);
        }
        Page page = new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page dbPage = memberMapper.selectPage(page, queryWrapper);
        return new PageResult(dbPage.getTotal(),dbPage.getRecords());
    }

    @Override
    public List<MemberEchartsVo> countMemberBySex() {
        return memberMapper.countMemberBySex();
    }

    @Override
    public List<MemberEchartsVo> countMemberByRegTime(String year) {
        return memberMapper.countMemberByRegTime(year);
    }

    @Override
    public int getTodayNewMember() {
        try {
            String today = DateUtils.parseDate2String(new Date());
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("regTime",today);
            return memberMapper.selectCount(queryWrapper);

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int getTotalMember() {
        return memberMapper.selectCount(null);
    }

    @Override
    public int getThisWeekNewMember() {
        try {
            String today = DateUtils.parseDate2String(new Date());
            String monday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.between("regTime",monday,today);
            return memberMapper.selectCount(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int getThisMonthNewMember() {
        try {
            String today = DateUtils.parseDate2String(new Date());
            String month = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.between("regTime",month,today);
            return memberMapper.selectCount(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Member selectPhone(String phoneNumber) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("phoneNumber",phoneNumber);
        List<Member> members = memberMapper.selectList(memberQueryWrapper);
        for (Member member : members) {
            return member;
        }
        return null;
    }

    @Override
    public Member loginMember(String phoneNumber, String password) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("phoneNumber",phoneNumber).eq("password",password).eq("status","正常");
        Member member = memberMapper.selectOne(memberQueryWrapper);
        return member;
    }
}
